package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.dto.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class SHENXIANAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, String> param = new HashMap<>();
        param.put("gameId", "4");
        // gameList
        // gamefilter
        String response = HttpUtil.doGet("http://interface.sxds.com/api/comm/gamefilter", param, null);
        String data = JSONObject.parseObject(response).getString("data");
        JSONObject datas = JSONObject.parseObject(data);
        Set<String> set = datas.keySet();
        List<JSONObject> list = new ArrayList();
        for(String key : set){
            Object value = datas.get(key);
            list.add(JSONObject.parseObject(value.toString()));
        }
        List<JSONObject> collect = list.stream().filter(d -> d.getString("fieldKey").endsWith("Isd")).collect(Collectors.toList());
        System.out.println(collect);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String response = HttpRequest.get("http://interface.sxds.com/api/comm/gameList").execute().body();
        JSONObject resJson = JSONObject.parseObject(response);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, String> param = new HashMap<>();
        param.put("gameId", gameId);
        // gameList
        // gamefilter
        String response = HttpUtil.doGet("http://interface.sxds.com/api/comm/gamefilter", param, null);
        String data = JSONObject.parseObject(response).getString("data");
        JSONObject datas = JSONObject.parseObject(data);
        Set<String> set = datas.keySet();
        List<JSONObject> list = new ArrayList();
        for(String key : set){
            Object value = datas.get(key);
            list.add(JSONObject.parseObject(value.toString()));
        }
        List<JSONObject> collect = list.stream().filter(d -> d.getString("fieldKey").equals("areaId")).collect(Collectors.toList());
        JSONObject jsonObject1 = collect.get(0);
        String options = jsonObject1.getString("options");
        List<JSONObject> options2 = JSONObject.parseArray(options, JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : options2) {
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(UUID.randomUUID().toString());
            interServer.setId(datum.getString("specialKey")+","+datum.getString("specialId"));
            interServer.setName(datum.getString("specialName"));
            String servers = datum.getString("secondOptions");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    interServer1.setUuid(UUID.randomUUID().toString());
                    String id1 = object.getString("twoSpecialKey")+","+object.getString("twoSpecialId");
                    String title1 = object.getString("twoSpecialName");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Map<String, String> param = new HashMap<>();
        param.put("gameId", gameId);
        // gameList
        // gamefilter
        String response = HttpUtil.doGet("http://interface.sxds.com/api/comm/gamefilter", param, null);
        String data = JSONObject.parseObject(response).getString("data");
        JSONObject datas = JSONObject.parseObject(data);
        Set<String> set = datas.keySet();
        List<JSONObject> list = new ArrayList();
        for(String key : set){
            Object value = datas.get(key);
            list.add(JSONObject.parseObject(value.toString()));
        }
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = list.stream().filter(d -> d.getString("fieldKey").endsWith("Id")||d.getString("fieldKey").endsWith("it")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = list.stream().filter(d -> d.getString("fieldKey").endsWith("Isd")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = list.stream().filter(d -> d.getString("fieldKey").equals("multi-input")).collect(Collectors.toList());
        }
        if(CollectionUtils.isNotEmpty(collect)) {
            for (JSONObject object : collect) {
                String string = object.getString("options");
                JSONArray jsonArray = JSONArray.parseArray(string);
                InterAttrRes interAttrRes = new InterAttrRes();
                interAttrRes.setUuid(UUID.randomUUID().toString());
                interAttrRes.setId(object.getString("fieldKey"));
                interAttrRes.setName(object.getString("fieldName"));
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUID.randomUUID().toString());
                    interAttrRes1.setId(jsonObject1.getString("specialId")+","+jsonObject1.getString("specialKey"));
                    interAttrRes1.setName(jsonObject1.getString("specialName"));
                    String secondOptions = jsonObject1.getString("secondOptions");
                    JSONArray jsonArray2 = JSONArray.parseArray(secondOptions);
                    List<InterAttrRes> interAttrResList2 = new ArrayList<>();
                    for(Object p : jsonArray2){
                        JSONObject jsonObject2 = JSONObject.parseObject(p.toString());
                        InterAttrRes interAttrRes2 = new InterAttrRes();
                        interAttrRes2.setUuid(UUID.randomUUID().toString());
                        interAttrRes2.setId(jsonObject2.getString("twoSpecialId")+","+jsonObject2.getString("twoSpecialKey"));
                        interAttrRes2.setName(jsonObject2.getString("twoSpecialName"));
                        interAttrResList2.add(interAttrRes2);
                    }
                    interAttrRes1.setInterServerRes(interAttrResList2);
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }
}